package com.kizuki.io;

import java.io.*;

/**
 * 装饰器模式
 */
public class TestBuffered {

    public static void main(String[] args) throws IOException {
        File f = new File("d:/java/learnio/TestIOBuffered.txt");
        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }
        if (!f.exists()) {
            f.createNewFile();
        }

        try (Writer bufferedWriter = new BufferedWriter(new FileWriter(f))) {

            bufferedWriter.write("hello 你好\n");
            bufferedWriter.write("hello 你好2\n");
            bufferedWriter.flush(); // 强制把缓存中的数据写入硬盘，无论缓存是否已满
            bufferedWriter.write("hello 你好3\n");
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileReader fileReader = new FileReader(f);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        while (true) {
            String line = bufferedReader.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }
    }
}
